﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T4UtilityBelt.DataDesigns;
using T4UtilityBelt.Serializers;
using T4UtilityBelt.UxDesigns.Controls.Renderers;

using T4UtilityBelt.Writers;

namespace T4UtilityBelt.UxDesigns
{
    public interface IContentElement
    {
        void FixModel(Entity entity);

        void RenderUiXml(CodeWriter writer);

        void RenderViewModelCs(CodeWriter writer);

        string ToStringUiXml(Platforms platform);

        string ToStringViewModelCs(Platforms platform);

    }

    public abstract class ContentElement : IContentElement,  IXShouldSerialize
    {
        protected ContentElement()
        {
            ShouldSerialize = true;
        }

        [XIgnore]
        public bool ShouldSerialize { get; set; }

        public virtual void FixModel(Entity entity)
        {
        }

        public abstract void RenderUiXml(CodeWriter writer);

        public abstract void RenderViewModelCs(CodeWriter writer);

        public string ToStringUiXml(Platforms platform)
        {
            CodeWriter writer = new CodeWriter(platform);
            RenderUiXml(writer);
            return writer.ToString();
        }

        public string ToStringViewModelCs(Platforms platform)
        {
            CodeWriter writer = new CodeWriter(platform);
            RenderViewModelCs(writer);
            return writer.ToString();
        }

    }

    public abstract class ContentElement<TControl, TRenderer> : ContentElement
        where TControl : ContentElement<TControl, TRenderer>
        where TRenderer : class, IContentElementRenderer<TControl, TRenderer>
    {
        #region Rendering

        protected TRenderer CreateControlRenderer(Platforms platform)
        {
            return RendererHelper.CreateRenderer<TRenderer>(platform);
        }

        #endregion Rendering

        #region Methods

        //public override void FixModel(Entity entity)
        //{
        //    foreach (IControl c in Controls)
        //    {
        //        c.FixModel(entity);
        //    }
        //}

        public sealed override void RenderViewModelCs(CodeWriter writer)
        {
            CreateControlRenderer(writer.Platform).RenderUiCode(this as TControl, writer);
        }

        public sealed override void RenderUiXml(CodeWriter writer)
        {
            CreateControlRenderer(writer.Platform).RenderUiXml(this as TControl, writer);
        }

        public void RenderUiXmlBegin(CodeWriter writer)
        {
            CreateControlRenderer(writer.Platform).RenderUiXmlBegin(this as TControl, writer);
        }

        public void RenderUiXmlEnd(CodeWriter writer)
        {
            CreateControlRenderer(writer.Platform).RenderUiXmlEnd(this as TControl, writer);
        }

        #endregion Methods
    }

}
